home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n03 / dflt10.exe / WINDOW.C < prev   
Encoding:
C/C++ Source or Header  |  1991-12-14  |  15.5 KB  |  522 lines

  1. /* ---------- window.c ------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. WINDOW inFocus = NULL;
  6.  
  7. int foreground, background;   /* current video colors */
  8.  
  9. static void TopLine(WINDOW, int, RECT);
  10.  
  11. /* --------- create a window ------------ */
  12. WINDOW CreateWindow(
  13.     CLASS class,              /* class of this window       */
  14.     char *ttl,                /* title or NULL              */
  15.     int left, int top,        /* upper left coordinates     */
  16.     int height, int width,    /* dimensions                 */
  17.     void *extension,          /* pointer to additional data */
  18.     WINDOW parent,            /* parent of this window      */
  19.     int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
  20.     int attrib)               /* window attribute           */
  21. {
  22.     WINDOW wnd = calloc(1, sizeof(struct window));
  23.     get_videomode();
  24.     if (wnd != NULL)    {
  25.         int base;
  26.         /* ----- height, width = -1: fill the screen ------- */
  27.         if (height == -1)
  28.             height = SCREENHEIGHT;
  29.         if (width == -1)
  30.             width = SCREENWIDTH;
  31.         /* ----- coordinates -1, -1 = center the window ---- */
  32.         if (left == -1)
  33.             wnd->rc.lf = (SCREENWIDTH-width)/2;
  34.         else
  35.             wnd->rc.lf = left;
  36.         if (top == -1)
  37.             wnd->rc.tp = (SCREENHEIGHT-height)/2;
  38.         else
  39.             wnd->rc.tp = top;
  40.         wnd->attrib = attrib;
  41.         if (ttl != NULL)
  42.             AddAttribute(wnd, HASTITLEBAR);
  43.         if (wndproc == NULL)
  44.             wnd->wndproc = classdefs[class].wndproc;
  45.         else
  46.             wnd->wndproc = wndproc;
  47.         /* ---- derive attributes of base classes ---- */
  48.         base = class;
  49.         while (base != -1)    {
  50.             AddAttribute(wnd, classdefs[base].attrib);
  51.             base = classdefs[base].base;
  52.         }
  53.         if (parent && !TestAttribute(wnd, NOCLIP))    {
  54.             /* -- keep upper left within borders of parent - */
  55.             wnd->rc.lf = max(wnd->rc.lf,GetClientLeft(parent));
  56.             wnd->rc.tp = max(wnd->rc.tp,GetClientTop(parent));
  57.         }
  58.         wnd->class = class;
  59.         wnd->extension = extension;
  60.         wnd->rc.rt = GetLeft(wnd)+width-1;
  61.         wnd->rc.bt = GetTop(wnd)+height-1;
  62.         wnd->ht = height;
  63.         wnd->wd = width;
  64.         if (ttl != NULL)
  65.             InsertTitle(wnd, ttl);
  66.         wnd->nextfocus = wnd->prevfocus = wnd->dFocus = NULL;
  67.         wnd->parent = parent;
  68.         wnd->oldcondition = wnd->condition = ISRESTORED;
  69.         wnd->RestoredRC = wnd->rc;
  70.         wnd->PrevKeyboard = wnd->PrevMouse = NULL;
  71.         SendMessage(wnd, CREATE_WINDOW, 0, 0);
  72.         InitWindowColors(wnd);
  73.         if (isVisible(wnd))
  74.             SendMessage(wnd, SHOW_WINDOW, 0, 0);
  75.     }
  76.     return wnd;
  77. }
  78.  
  79. /* -------- add a title to a window --------- */
  80. void AddTitle(WINDOW wnd, char *ttl)
  81. {
  82.     InsertTitle(wnd, ttl);
  83.     SendMessage(wnd, BORDER, 0, 0);
  84. }
  85.  
  86. /* ----- insert a title into a window ---------- */
  87. void InsertTitle(WINDOW wnd, char *ttl)
  88. {
  89.     if ((wnd->title=realloc(wnd->title,strlen(ttl)+1)) != NULL)
  90.         strcpy(wnd->title, ttl);
  91. }
  92.  
  93. static unsigned char line[300];
  94.  
  95. /* ------ write a line to video window client area ------ */
  96. void writeline(WINDOW wnd, char *str, int x, int y, int pad)
  97. {
  98.     char *cp;
  99.     int len;
  100.     int dif;
  101.     char *wline = calloc(1, 200);
  102.  
  103.     if (wline != NULL)    {
  104.         len = LineLength(str);
  105.         dif = strlen(str) - len;
  106.         strncpy(wline, str, ClientWidth(wnd) + dif);
  107.         if (pad)    {
  108.             cp = wline+strlen(wline);
  109.             while (len++ < ClientWidth(wnd)-x)
  110.                 *cp++ = ' ';
  111.         }
  112.         wputs(wnd, wline, x, y);
  113.         free(wline);
  114.     }
  115. }
  116.  
  117. RECT AdjustRectangle(WINDOW wnd, RECT rc)
  118. {
  119.     /* -------- adjust the rectangle ------- */
  120.     if (TestAttribute(wnd, HASBORDER))    {
  121.         if (RectLeft(rc) == 0)
  122.             --rc.rt;
  123.         else if (RectLeft(rc) < RectRight(rc) &&
  124.                 RectLeft(rc) < WindowWidth(wnd)+1)
  125.             --rc.lf;
  126.     }
  127.     if (TestAttribute(wnd, HASBORDER | HASTITLEBAR))    {
  128.         if (RectTop(rc) == 0)
  129.             --rc.bt;
  130.         else if (RectTop(rc) < RectBottom(rc) &&
  131.                 RectTop(rc) < WindowHeight(wnd)+1)
  132.             --rc.tp;
  133.     }
  134.     RectRight(rc) = max(RectLeft(rc),
  135.                         min(RectRight(rc),WindowWidth(wnd)));
  136.     RectBottom(rc) = max(RectTop(rc),
  137.                         min(RectBottom(rc),WindowHeight(wnd)));
  138.     return rc;
  139. }
  140.  
  141. /* -------- display a window's title --------- */
  142. void DisplayTitle(WINDOW wnd, RECT *rcc)
  143. {
  144.     if (GetTitle(wnd) != NULL)    {
  145.         int tlen = min(strlen(GetTitle(wnd)), WindowWidth(wnd)-2);
  146.         int tend = WindowWidth(wnd)-3-BorderAdj(wnd);
  147.         RECT rc;
  148.  
  149.         if (rcc == NULL)
  150.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  151.         else
  152.             rc = *rcc;
  153.         rc = AdjustRectangle(wnd, rc);
  154.  
  155.         if (SendMessage(wnd, TITLE, (PARAM) rcc, 0))    {
  156.             if (wnd == inFocus)    {
  157.                 foreground = cfg.clr[TITLEBAR] [HILITE_COLOR] [FG];
  158.                 background = cfg.clr[TITLEBAR] [HILITE_COLOR] [BG];
  159.             }
  160.             else    {
  161.                 foreground = cfg.clr[TITLEBAR] [STD_COLOR] [FG];
  162.                 background = cfg.clr[TITLEBAR] [STD_COLOR] [BG];
  163.             }
  164.             memset(line,' ',WindowWidth(wnd));
  165. #ifdef INCLUDE_MINIMIZE
  166.             if (wnd->condition != ISMINIMIZED)
  167. #endif
  168.                 strncpy(line + ((WindowWidth(wnd)-2 - tlen) / 2),
  169.                     wnd->title, tlen);
  170.             if (TestAttribute(wnd, CONTROLBOX))
  171.                 line[2-BorderAdj(wnd)] = CONTROLBOXCHAR;
  172.             if (TestAttribute(wnd, MINMAXBOX))    {
  173.                 switch (wnd->condition)    {
  174.                     case ISRESTORED:
  175. #ifdef INCLUDE_MAXIMIZE
  176.                         line[tend+1] = MAXPOINTER;
  177. #endif
  178. #ifdef INCLUDE_MINIMIZE
  179.                         line[tend]   = MINPOINTER;
  180. #endif
  181.                         break;
  182. #ifdef INCLUDE_MINIMIZE
  183.                     case ISMINIMIZED:
  184.                         line[tend+1] = MAXPOINTER;
  185.                         break;
  186. #endif
  187. #ifdef INCLUDE_MAXIMIZE
  188.                     case ISMAXIMIZED:
  189. #ifdef INCLUDE_MINIMIZE
  190.                         line[tend]   = MINPOINTER;
  191. #endif
  192. #ifdef INCLUDE_RESTORE
  193.                         line[tend+1] = RESTOREPOINTER;
  194. #endif
  195.                         break;
  196. #endif
  197.                     default:
  198.                         break;
  199.                 }
  200.             }
  201.             line[RectRight(rc)+1] = line[tend+3] = '\0';
  202.             writeline(wnd, line+RectLeft(rc),
  203.                            RectLeft(rc)+BorderAdj(wnd),
  204.                            0,
  205.                            FALSE);
  206.         }
  207.     }
  208. }
  209.  
  210. /* --- display right border shadow character of a window --- */
  211. static void near shadow_char(WINDOW wnd, int y)
  212. {
  213.     int fg = foreground;
  214.     int bg = background;
  215.     int x = WindowWidth(wnd);
  216.     int c = videochar(GetLeft(wnd)+x, GetTop(wnd)+y);
  217.  
  218.     if (TestAttribute(wnd, SHADOW) == 0)
  219.         return;
  220.     foreground = LIGHTGRAY;
  221.     background = BLACK;
  222.     wputch(wnd, c, x, y);
  223.     foreground = fg;
  224.     background = bg;
  225. }
  226.  
  227. /* --- display the bottom border shadow line for a window -- */
  228. static void near shadowline(WINDOW wnd, RECT rc)
  229. {
  230.     int i;
  231.     int y = GetBottom(wnd)+1;
  232.     int fg = foreground;
  233.     int bg = background;
  234.  
  235.     if ((TestAttribute(wnd, SHADOW)) == 0)
  236.         return;
  237.     for (i = 0; i < WindowWidth(wnd)+1; i++)
  238.         line[i] = videochar(GetLeft(wnd)+i, y);
  239.     line[i] = '\0';
  240.     foreground = LIGHTGRAY;
  241.     background = BLACK;
  242.     line[RectRight(rc)+1] = '\0';
  243.     if (RectLeft(rc) == 0)
  244.         rc.lf++;
  245.     ClipString++;
  246.     wputs(wnd, line+RectLeft(rc), RectLeft(rc),
  247.         WindowHeight(wnd));
  248.     --ClipString;
  249.     foreground = fg;
  250.     background = bg;
  251. }
  252.  
  253. /* ------- display a window's border ----- */
  254. void RepaintBorder(WINDOW wnd, RECT *rcc)
  255. {
  256.     int y;
  257.     unsigned int lin, side, ne, nw, se, sw;
  258.     RECT rc, clrc;
  259.  
  260.     if (!TestAttribute(wnd, HASBORDER))
  261.         return;
  262.     if (rcc == NULL)    {
  263.         rc = RelativeWindowRect(wnd, WindowRect(wnd));
  264.         if (TestAttribute(wnd, SHADOW))    {
  265.             rc.rt++;
  266.             rc.bt++;
  267.         }
  268.     }
  269.     else
  270.         rc = *rcc;
  271.     clrc = AdjustRectangle(wnd, rc);
  272.  
  273.     if (wnd == inFocus)    {
  274.         lin  = FOCUS_LINE;
  275.         side = FOCUS_SIDE;
  276.         ne   = FOCUS_NE;
  277.         nw   = FOCUS_NW;
  278.         se   = FOCUS_SE;
  279.         sw   = FOCUS_SW;
  280.     }
  281.     else    {
  282.         lin  = LINE;
  283.         side = SIDE;
  284.         ne   = NE;
  285.         nw   = NW;
  286.         se   = SE;
  287.         sw   = SW;
  288.     }
  289.     line[WindowWidth(wnd)] = '\0';
  290.     /* ---------- window title ------------ */
  291.     if (TestAttribute(wnd, HASTITLEBAR))
  292.         if (RectTop(rc) == 0)
  293.             if (RectLeft(rc) < WindowWidth(wnd)-BorderAdj(wnd))
  294.                 DisplayTitle(wnd, &rc);
  295.     foreground = FrameForeground(wnd);
  296.     background = FrameBackground(wnd);
  297.     /* -------- top frame corners --------- */
  298.     if (RectTop(rc) == 0)    {
  299.         if (RectLeft(rc) == 0)
  300.             wputch(wnd, nw, 0, 0);
  301.         if (RectLeft(rc) < WindowWidth(wnd))    {
  302.             if (RectRight(rc) >= WindowWidth(wnd)-1)
  303.                 wputch(wnd, ne, WindowWidth(wnd)-1, 0);
  304.             TopLine(wnd, lin, clrc);
  305.         }
  306.     }
  307.  
  308.     /* ----------- window body ------------ */
  309.     for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  310.         int ch;
  311.         if (y == 0 || y >= WindowHeight(wnd)-1)
  312.             continue;
  313.         if (RectLeft(rc) == 0)
  314.             wputch(wnd, side, 0, y);
  315.         if (RectLeft(rc) < WindowWidth(wnd) &&
  316.                 RectRight(rc) >= WindowWidth(wnd)-1)    {
  317.             if (TestAttribute(wnd, VSCROLLBAR))
  318.                 ch = (    y == 1 ? UPSCROLLBOX      :
  319.                           y == WindowHeight(wnd)-2  ?
  320.                                 DOWNSCROLLBOX       :
  321.                           y-1 == wnd->VScrollBox    ?
  322.                                 SCROLLBOXCHAR       :
  323.                           SCROLLBARCHAR );
  324.             else
  325.                 ch = side;
  326.             wputch(wnd, ch, WindowWidth(wnd)-1, y);
  327.         }
  328.         if (RectRight(rc) == WindowWidth(wnd))
  329.             shadow_char(wnd, y);
  330.     }
  331.  
  332.     if (RectTop(rc) <= WindowHeight(wnd)-1 &&
  333.             RectBottom(rc) >= WindowHeight(wnd)-1)    {
  334.         /* -------- bottom frame corners ---------- */
  335.         if (RectLeft(rc) == 0)
  336.             wputch(wnd, sw, 0, WindowHeight(wnd)-1);
  337.         if (RectLeft(rc) < WindowWidth(wnd) &&
  338.                 RectRight(rc) >= WindowWidth(wnd)-1)
  339.             wputch(wnd, se, WindowWidth(wnd)-1,
  340.                 WindowHeight(wnd)-1);
  341.  
  342.  
  343.         if (wnd->StatusBar == NULL)    {
  344.             /* ----------- bottom line ------------- */
  345.             memset(line,lin,WindowWidth(wnd)-1);
  346.             if (TestAttribute(wnd, HSCROLLBAR))    {
  347.                 line[0] = LEFTSCROLLBOX;
  348.                 line[WindowWidth(wnd)-3] = RIGHTSCROLLBOX;
  349.                 memset(line+1, SCROLLBARCHAR, WindowWidth(wnd)-4);
  350.                 line[wnd->HScrollBox] = SCROLLBOXCHAR;
  351.             }
  352.             line[WindowWidth(wnd)-2] = line[RectRight(rc)] = '\0';
  353.             if (RectLeft(rc) != RectRight(rc) ||
  354.             (RectLeft(rc) && RectLeft(rc) < WindowWidth(wnd)-1))
  355.                 writeline(wnd,
  356.                     line+(RectLeft(clrc)),
  357.                     RectLeft(clrc)+1,
  358.                     WindowHeight(wnd)-1,
  359.                     FALSE);
  360.         }
  361.         if (RectRight(rc) == WindowWidth(wnd))
  362.             shadow_char(wnd, WindowHeight(wnd)-1);
  363.     }
  364.     if (RectBottom(rc) == WindowHeight(wnd))
  365.         /* ---------- bottom shadow ------------- */
  366.         shadowline(wnd, rc);
  367. }
  368.  
  369. static void TopLine(WINDOW wnd, int lin, RECT rc)
  370. {
  371.     if (TestAttribute(wnd, HASMENUBAR))
  372.         return;
  373.     if (TestAttribute(wnd, HASTITLEBAR) && GetTitle(wnd))
  374.         return;
  375.     if (RectLeft(rc) == 0)    {
  376.         RectLeft(rc) += BorderAdj(wnd);
  377.         RectRight(rc) += BorderAdj(wnd);
  378.     }
  379.     if (RectRight(rc) < WindowWidth(wnd)-1)
  380.         RectRight(rc)++;
  381.  
  382.     if (RectLeft(rc) < RectRight(rc))    {
  383.         /* ----------- top line ------------- */
  384.         memset(line,lin,WindowWidth(wnd)-1);
  385.         if (TestAttribute(wnd, CONTROLBOX))    {
  386.             strncpy(line+1, "   ", 3);
  387.             *(line+2) = CONTROLBOXCHAR;
  388.         }
  389.         line[RectRight(rc)] = '\0';
  390.         writeline(wnd, line+RectLeft(rc),
  391.             RectLeft(rc), 0, FALSE);
  392.     }
  393. }
  394.  
  395. /* ------ clear the data space of a window -------- */
  396. void ClearWindow(WINDOW wnd, RECT *rcc, int clrchar)
  397. {
  398.     if (isVisible(wnd))    {
  399.         int y;
  400.         RECT rc;
  401.  
  402.         if (rcc == NULL)
  403.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  404.         else
  405.             rc = *rcc;
  406.  
  407.         if (RectLeft(rc) == 0)
  408.             RectLeft(rc) = BorderAdj(wnd);
  409.         if (RectRight(rc) > WindowWidth(wnd)-1)
  410.             RectRight(rc) = WindowWidth(wnd)-1;
  411.         SetStandardColor(wnd);
  412.         memset(line, clrchar, sizeof line);
  413.         line[RectRight(rc)+1] = '\0';
  414.         for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  415.             if (y < TopBorderAdj(wnd) ||
  416.                     y > ClientHeight(wnd)+
  417.                         (TestAttribute(wnd, HASMENUBAR) ? 1 : 0))
  418.                 continue;
  419.             writeline(wnd,
  420.                 line+(RectLeft(rc)),
  421.                 RectLeft(rc),
  422.                 y,
  423.                 FALSE);
  424.         }
  425.     }
  426. }
  427.  
  428. /* -- adjust a window's rectangle to clip it to its parent - */
  429. static RECT near ClipRect(WINDOW wnd)
  430. {
  431.     RECT rc;
  432.     rc = wnd->rc;
  433.     if (TestAttribute(wnd, SHADOW))    {
  434.         RectBottom(rc)++;
  435.         RectRight(rc)++;
  436.     }
  437.     return ClipRectangle(wnd, rc);
  438. }
  439.  
  440. /* -- get the video memory that is to be used by a window -- */
  441. void GetVideoBuffer(WINDOW wnd)
  442. {
  443.     RECT rc;
  444.     int ht;
  445.     int wd;
  446.  
  447.     rc = ClipRect(wnd);
  448.     ht = RectBottom(rc) - RectTop(rc) + 1;
  449.     wd = RectRight(rc) - RectLeft(rc) + 1;
  450.     wnd->videosave = realloc(wnd->videosave, (ht * wd * 2));
  451.     get_videomode();
  452.     if (wnd->videosave != NULL)
  453.         getvideo(rc, wnd->videosave);
  454. }
  455.  
  456. /* --- restore the video memory that was used by a window -- */
  457. void RestoreVideoBuffer(WINDOW wnd)
  458. {
  459.     if (wnd->videosave != NULL)    {
  460.         RECT rc;
  461.         rc = ClipRect(wnd);
  462.         storevideo(rc, wnd->videosave);
  463.         free(wnd->videosave);
  464.         wnd->videosave = NULL;
  465.     }
  466. }
  467.  
  468. /* ------ compute the logical line length of a window ------ */
  469. int LineLength(char *ln)
  470. {
  471.     int len = strlen(ln);
  472.     char *cp = ln;
  473.     while ((cp = strchr(cp, CHANGECOLOR)) != NULL)    {
  474.         cp++;
  475.         len -= 3;
  476.     }
  477.     cp = ln;
  478.     while ((cp = strchr(cp, RESETCOLOR)) != NULL)    {
  479.         cp++;
  480.         --len;
  481.     }
  482.     return len;
  483. }
  484.  
  485. void InitWindowColors(WINDOW wnd)
  486. {
  487.     int fbg,col;
  488.     int cls = GetClass(wnd);
  489.     /* window classes without assigned colors inherit parent's colors */
  490.     if (cfg.clr[cls][0][0] == 0xff && GetParent(wnd) != NULL)
  491.         cls = GetClass(GetParent(wnd));
  492.     /* ---------- set the colors ---------- */
  493.     for (fbg = 0; fbg < 2; fbg++)
  494.         for (col = 0; col < 4; col++)
  495.             wnd->WindowColors[col][fbg] = cfg.clr[cls][col][fbg];
  496. }
  497.  
  498. void PutWindowChar(WINDOW wnd, int c, int x, int y)
  499. {
  500.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))
  501.         wputch(wnd, c, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  502. }
  503.  
  504. void PutWindowLine(WINDOW wnd, void *s, int x, int y)
  505. {
  506.     int saved = FALSE, sv;
  507.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))    {
  508.         char *en = (char *)s+ClientWidth(wnd)-x;
  509.         if (strlen(s)+x > ClientWidth(wnd))    {
  510.             sv = *en;
  511.             *en = '\0';
  512.             saved = TRUE;
  513.         }
  514.         ClipString++;
  515.         wputs(wnd, s, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  516.         --ClipString;
  517.         if (saved)
  518.             *en = sv;
  519.     }
  520. }
  521.  
  522.